package logic

import (
	"context"
	"good_srv/good"
	"good_srv/goodclient"
	"good_srv/internal/svc"
	"good_srv/models"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodListLogic {
	return &GoodListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 商品审核列表
func (l *GoodListLogic) GoodList(in *good.GoodListRequest) (*good.GoodListResponse, error) {
	// todo: add your logic here and delete this line

	var goods []models.GoodInfo
	page := in.Page
	if page <= 0 {
		page = 1
	}

	pageSize := in.PageSize
	switch {
	case pageSize > 100:
		pageSize = 100
	case pageSize <= 0:
		pageSize = 10
	}

	offset := (page - 1) * pageSize
	models.DB.Model(&models.Good{}).
		Select("goods.title,goods.price,goods.stock,goods.image,users.phone,types.name,looks.num").
		Joins("left join types on goods.type_id = types.id").
		Joins("left join users on goods.user_id = users.id").
		Joins("left join looks on goods.look_id = looks.id").
		Offset(int(offset)).Limit(int(pageSize)).
		Where("checkout_status=0 and status=0").Find(&goods)
	var goodlist []*goodclient.GoodList
	for _, g := range goods {
		list := goodclient.GoodList{
			Title:  g.Title,
			Price:  float32(g.Price),
			UserId: in.UserId,
			Stock:  int64(g.Stock),
			TypeId: in.TypeId,
			Image:  g.Image,
			LookId: in.LookId,
			Phone:  in.Phone,
		}
		goodlist = append(goodlist, &list)
	}
	return &good.GoodListResponse{
		List: goodlist,
	}, nil
}
